﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM

using System.Collections.Generic;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.OrderFlowManage;
using YunSpace.Domain.IRepository.OrderFlowManage;
using YunSpace.Domain.ViewModel.WorkFlow;
using YunSpace.Domain.ViewModel.ClientManager;

#endregion

namespace YunSpace.Repository.OrderFlowManage
{
    public class ClientOrderrecommendRepository : RepositoryBase<ClientOrderrecommendEntity>, IClientOrderrecommendRepository
    {
        public List<MyAuditViewModel> GetFlowManagerViewModelList(Pagination pagination, string keyValue)
        {
            var sql = $@"
                SELECT o.F_Id AS F_SiteClientOrderRecommendId, o.F_OrderId, o.F_State AS F_ScheduleState, s.title AS SiteTitle, s2.title AS SpaceTitle
                FROM Site_Client_OrderRecommend AS o
                LEFT JOIN SL_Site AS s ON s.F_Id = o.F_SiteId
                LEFT JOIN SL_Space AS s2 ON s2.F_Id = o.F_SpaceId
                WHERE o.F_OrderId = '{keyValue}' AND (o.F_DeleteMark IS NULL OR o.F_DeleteMark <> 1) AND (s.F_DeleteMark IS NULL OR s.F_DeleteMark <> 1) AND (s2.F_DeleteMark IS NULL OR s2.F_DeleteMark <> 1)
                ";

            pagination.records = GetFlowManagerViewModelCount(keyValue);

            var startNo = pagination.rows * (pagination.page - 1);
            var endNo = startNo + pagination.rows;

            sql = $"{sql} ORDER BY o.{pagination.sidx} LIMIT {startNo},{endNo};";

            using (var db = new RepositoryBase())
            {
                var result = db.FindList<MyAuditViewModel>(sql);
                return result;
            }
        }

        private int GetFlowManagerViewModelCount(string keyValue)
        {
            var sql = $@"
                SELECT COUNT(1)
                FROM Site_Client_OrderRecommend AS o
                LEFT JOIN SL_Site AS s ON s.F_Id = o.F_SiteId
                LEFT JOIN SL_Space AS s2 ON s2.F_Id = o.F_SpaceId
                WHERE o.F_OrderId = '{keyValue}' AND(o.F_DeleteMark IS NULL OR o.F_DeleteMark <> 1) AND(s.F_DeleteMark IS NULL OR s.F_DeleteMark <> 1) AND(s2.F_DeleteMark IS NULL OR s2.F_DeleteMark <> 1)";

            return Count(sql);
        }
    }
}

